package com.itheima.mobileController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MobileLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-30 19:21
 **/
@RestController
@RequestMapping("login")
public class MobileLoginController {


    @Autowired
    private JedisPool jedisPool;
    @Reference
    private MobileLoginService mobileLoginService;

    @RequestMapping("loginCheck")
    public Result check(@RequestBody Map map){
        //从resdis中获取验证码并进行比较
        try {
            String telephone = (String)map.get("telephone");
            String validateCode = (String)map.get("validateCode");
            String redisCode = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
            if (redisCode == null || !redisCode.equals(validateCode)){
                return new Result(false, MessageConstant.LOGIN_ERROR);
            }
            //验证码正确,查询用户是否是会员,不是会员则加入会员,并登陆成功
            boolean flag = mobileLoginService.checkMember(telephone);
            if (!flag) {
                mobileLoginService.addMember(telephone);
            }
            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.LOGIN_ERROR);
        }
    }


}
